package com.example.server.controller;

import com.example.server.entity.Coupon;
import com.example.server.entity.Order;
import com.example.server.entity.OrderItem;
import com.example.server.redis.RedisService;
import com.example.server.service.CartItemService;
import com.example.server.service.CouponService;
import com.example.server.service.OrderItemService;
import com.example.server.service.OrderService;
import com.example.server.vo.MessageOrder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("order")

public class OrderController extends BaseController{

    public OrderController(OrderService orderService, OrderItemService orderItemService, CouponService couponService, RedisService redisService, CartItemService cartItemService) {
        this.orderService = orderService;
        this.orderItemService = orderItemService;
        this.couponService = couponService;
        this.redisService = redisService;
        this.cartItemService = cartItemService;
    }

    private final OrderService orderService;
    private final OrderItemService orderItemService;
    private final CouponService couponService;
    private final RedisService redisService;
    private final CartItemService cartItemService;



    int orderId;
    List<Integer> orderIdList = new ArrayList<>();
    /**
     * 详情页购买
     * @param map
     * @return
     */
    @PostMapping("create")
    private int create(@RequestBody Map<String,Object> map){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));
        int sid = orderService.queryNum("querySid",pid);
        System.out.println(sid);

        Order order = new Order();
        order.setUid(uid);
        order.setSid(sid);
        order.setAid(Integer.parseInt((String) map.get("aid")) );
        double total = Double.parseDouble((String) map.get("total"));
        order.setTotal(total);
        order.setSta(0);
        orderService.create(order);
        System.out.println(order.getId());
        orderId = order.getId();

        OrderItem orderItem = new OrderItem();
        orderItem.setOid(order.getId());
        orderItem.setPid(pid);
        orderItem.setNumber((int) map.get("number"));
        double unitPrice = Double.parseDouble((String) map.get("unit_price")) ;
        orderItem.setUnit_price(unitPrice);
        return orderItemService.create(orderItem);
    }

    @PostMapping("create2")
    private int create2(@RequestBody Map<String,OrderItem[]> map){
        List<OrderItem> list = new ArrayList<>();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int i = map.get("orders").length;
        double total = 0;
        System.out.println(i);
        for (int l = 0;l < i; l++){
            OrderItem orderItem = map.get("orders")[l];
            int cid = orderItem.getId();
            int pid = orderItem.getProduct().getId();
            int sid = orderService.queryNum("querySid",pid);
            System.out.println(orderItem);
            if(list.isEmpty()){
                Order order = new Order();
                order.setUid(uid);
                order.setSta(0);
                order.setAid(1);
                double total1 = orderItem.getNumber() * orderItem.getProduct().getNew_price();
                total = total1;
                order.setTotal(total1);
                order.setSid(sid);
                orderService.create(order);
                int id = order.getId();
                orderIdList.add(id);
                orderItem.setOid(id);
                System.out.println("###############################################"  + id);
                // ----------------------------------------------------------
                OrderItem orderItem1 = new OrderItem();
                orderItem1.setOid(id);
                orderItem1.setPid(orderItem.getProduct().getId());
                orderItem1.setNumber(orderItem.getNumber());
                orderItem1.setUnit_price(orderItem.getProduct().getNew_price());
                System.out.println("-----------------------------------" + orderItem1);
                orderItemService.create(orderItem1);
                cartItemService.removeOne("removeById",cid);
            }else {
                OrderItem orderItem2 = new OrderItem();
                boolean m = true;
                for (OrderItem orderItem1: list) {
                    int pid1 = orderItem1.getProduct().getId();
                    System.out.println("----------------------------------------" + pid1);
                    int sid1 = orderService.queryNum("querySid",pid1);
                    if(sid == sid1){
                        Order order = new Order();
                        int oid = orderItem1.getOid();
                        System.out.println("-----------------------------" + oid);
                        orderItem2.setOid(oid);
                        orderItem2.setPid(pid);
                        orderItem2.setNumber(orderItem.getNumber());
                        orderItem2.setUnit_price(orderItem.getProduct().getNew_price());
                        orderItemService.create(orderItem2);
                        double total2 = orderItem.getNumber() * orderItem.getProduct().getNew_price();
                        order.setId(oid);
                        order.setTotal(total + total2);
                        orderService.modifyOne("modifyTAndA",order);
                        cartItemService.removeOne("removeById",cid);
                        m = false;
                        break;
                    }
                }
                if(m){
                    Order order = new Order();
                    order.setUid(uid);
                    order.setSta(0);
                    order.setAid(1);
                    double total1 = orderItem.getNumber() * orderItem.getProduct().getNew_price();
                    total = total1;
                    order.setTotal(total1);
                    order.setSid(sid);
                    orderService.create(order);
                    // ----------------------------------------------------------
                    OrderItem orderItem1 = new OrderItem();
                    orderItem1.setOid(order.getId());
                    orderItem1.setPid(orderItem.getProduct().getId());
                    orderItem1.setNumber(orderItem.getNumber());
                    orderItem1.setUnit_price(orderItem.getProduct().getNew_price());
                    orderItemService.create(orderItem1);
                    cartItemService.removeOne("removeById",cid);
                    int id = order.getId();
                    orderIdList.add(id);
                    orderItem.setOid(id);
                }
            }
            list.add(orderItem);
        }
        return 1;
    }

    /**
     * 添加地址
     * @param map
     * @return
     */
    @PutMapping("modifyTAndA")
    private int modifyTAndA(@RequestBody Map<String,Object> map){
        int aid = Integer.parseInt((String) map.get("aid"));
        Order order = new Order();
        order.setAid(aid);
        for (int orderId:orderIdList) {
            order.setId(orderId);
            orderService.modifyOne("modifyTAndA",order);
        }
        return 1;
    }
    @PutMapping("modifyStaList")
    private int modifyStaList(){
        Order order = new Order();
        order.setSta(1);
        for (int orderId:orderIdList) {
            order.setId(orderId);
            orderService.modifyOne("modifySta1",order);
        }
        return 1;
    }

    /**
     **确认订单
     * @param coupon
     * @return
     */
    @PutMapping("modifySta1")
    private int modifySta1(@RequestBody Coupon coupon){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        Order order = new Order();
        order.setId(orderId);
        order.setSta(1);
        int i = orderService.modifyOne("modifySta1",order);
        if(i > 0 && coupon.getCode() != null){
            coupon.setUid(uid);
            couponService.modifyOne("modifyQ",coupon);
            int quantity = couponService.queryNum("queryQuantity1",coupon);
            if(quantity == 0){
                couponService.removeOne("delete",coupon);
            }
        }
        return i;
    }

    /**
     * 发货
     * @param order
     * @return
     */
    @PutMapping("modifySta2")
    private int modifySta2(@RequestBody Order order){
//        Order order = new Order();
//        order.setId(id);
        order.setSta(2);
        return orderService.modifyOne("modifySta",order);
    }
    @PutMapping("modifySta3")
    private int modifySta3(@RequestBody Order order){
        order.setSta(3);
        return orderService.modifyOne("modifySta",order);
    }
    @PutMapping("modifySta4")
    private int modifySta4(@RequestBody Order order){
        order.setSta(4);
        return orderService.modifyOne("modifySta",order);
    }

    @GetMapping("queryByUid")
    private List<Order> queryByUid(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        return orderService.queryList("queryByUid",uid);
    }

    @GetMapping("queryByUidAndSta")
    private List<Order> queryByUidAndSta(@RequestParam Map<String,Object> map){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        Order order = new Order();
        order.setUid(uid);
        int sta = Integer.parseInt((String) map.get("sta"));
        order.setSta(sta);
        return orderService.queryList("queryByUidAndSta",order);
    }

    @GetMapping("queryBySid")
    private List<Order> queryBySid(){
        int sid = Integer.parseInt(redisService.getSetString("storeId"));
        return orderService.queryList("queryBySid",sid);
    }
    @GetMapping("queryBySidAndSta")
    private List<MessageOrder> queryBySidAndSta(@RequestParam Map<String,String> map){
        int sid = Integer.parseInt(redisService.getSetString("storeId"));
        Order order = new Order();
        order.setSid(sid);
        int sta = Integer.parseInt(map.get("sta"));
        order.setSta(sta);
        List<Order> list = orderService.queryList("queryBySidAndSta",order);
        List<MessageOrder> list1 = new ArrayList<>();
        for (Order order1:list) {
            MessageOrder messageOrder = new MessageOrder();
            messageOrder.setId(order1.getId());
            messageOrder.setName(order1.getAddress().getName());
            messageOrder.setAddress(order1.getAddress().getAddress());
            messageOrder.setTelephone(order1.getAddress().getTelephone());
            messageOrder.setTotal(order1.getTotal());
            SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String targetStr = targetFormat.format(order1.getTime());
            messageOrder.setDate(targetStr);

            list1.add(messageOrder);
        }
        System.out.println(list1);
        return list1;
    }

    @DeleteMapping("deleteOrder")
    private int deleteOrder(){
        int id = orderId;
        orderItemService.removeOne("deleteOrderItem",id);
        return orderService.removeOne("deleteOrder",id);
    }

    @DeleteMapping("deleteOrderList")
    private int deleteOrderList(){
        for(int id:orderIdList){
            orderItemService.removeOne("deleteOrderItem",id);
            orderService.removeOne("deleteOrder",id);
        }
        return 1;
    }

    @GetMapping("queryById")
    private Order queryById(@RequestParam Map<String,String> map){
        int id = Integer.parseInt(map.get("id"));
        return orderService.queryById(id);
    }

    @DeleteMapping("removeById")
    private int removeById(@RequestParam Map<String,Object> map){
        int id = Integer.parseInt(map.get("id") + "");
        orderItemService.removeOne("deleteOrderItem",id);
        return orderService.removeOne("deleteOrder",id);
    }
}
